﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Diagnostics;
namespace VHProject新功能研究
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = "F://123综合文件/111.pdf";
            string desFile = "C://PDF/test12345.pdf";
            string bigfile = "F://123文档/2016PDF.pdf";
            Console.WriteLine("分割中...");
            //PDFSplit(sourceFile, "C://PDF");

            //CopyByPageNum1(sourceFile, desFile, 1, 5);
            //CopyByPageNum2(sourceFile, desFile, 1, 5);
            //CopyByPageNum3(sourceFile, desFile, new int[]{1,3,5});

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //PDFKit.PDFSplit(bigfile, "C://PDF","F://water.png",0,0);
            PDFKit.AddWatermarkImg(bigfile, null, "F://1.pdf", "F://water.png", 0, 0);
            stopwatch.Stop();
            Console.WriteLine("耗时 " + stopwatch.Elapsed.TotalSeconds+" 秒");
           
            Console.WriteLine("分割完毕");
            Console.ReadKey();
        }

        /// <summary>
        /// 将PDF文件指定页码范围(不可跳页)拷贝至新的PDF文件
        /// </summary>
        /// <param name="sourcePdfPath">PDF文件源路径</param>
        /// <param name="outputPdfPath">生成pdf文件目标路径</param>
        /// <param name="startPage">起始页码</param>
        /// <param name="endPage">结束页码</param>
        /// <returns>返回值判断是否执行成功</returns>
        public static bool CopyByPageNum1(string sourcePdfPath, string outputPdfPath, int startPage, int endPage) 
        { 
            PdfReader reader = null; 
            Document sourceDocument = null; 
            PdfCopy pdfCopyProvider = null; 
            PdfImportedPage importedPage = null;
            try 
            { 
                reader = new PdfReader(sourcePdfPath); 
                sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage)); 
                pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create)); 
                sourceDocument.Open(); 
                for (int i = startPage; i <= endPage; i++) 
                { 
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i); 
                    pdfCopyProvider.AddPage(importedPage);
                } 
                sourceDocument.Close();
                reader.Close();
                return true;
            } 
            catch (Exception ex) 
            {
                return false;
            } 
        }
        
        /// <summary>
        /// 将PDF文件指定页码范围(不可跳页)拷贝至新的PDF文件
        /// </summary>
        /// <param name="source">PDF文件源路径</param>
        /// <param name="target">生成PDF文件目标路径</param>
        /// <param name="StartPage">起始页码</param>
        /// <param name="EndPage">结束页码</param>
        /// <returns>返回值判断是否执行成功</returns>
        public static bool CopyByPageNum2(string source, string target, int StartPage, int EndPage) 
        { 
            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(source); 
            Document document = new Document(reader.GetPageSizeWithRotation(StartPage)); //创建document对象 // 
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(target, FileMode.Create)); //实例化document对象 
            document.Open(); 
            int rotation; 
            PdfContentByte cb = writer.DirectContent; 
            PdfImportedPage page; 
            for (int i = 1; i <= StartPage; i++) 
            {
                while (StartPage <= EndPage) 
                { 
                    document.SetPageSize(reader.GetPageSizeWithRotation(StartPage)); 
                    document.NewPage(); page = writer.GetImportedPage(reader, StartPage); 
                    rotation = reader.GetPageRotation(StartPage); 
                    if (rotation == 90 || rotation == 270) 
                    { 
                        document.NewPage(); 
                        if (rotation == 90) 
                        { 
                            cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(StartPage).Height); 
                        } 
                        if (rotation == 270) 
                        { 
                            cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(StartPage).Width, 0);
                        }
                    } 
                    else 
                    { 
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                StartPage++;
                } 
            }
            try 
            { 
                document.Close(); 
                reader = null;
                return true;
            }
            
            catch (Exception ex) 
            {
                return false;
            } 
        }

        /// <summary>
        /// 将PDF文件指定页码范围(可跳页)拷贝至新的PDF文件
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="pageNums"></param>
        /// <returns>返回值判断是否执行成功</returns>
        public static bool CopyByPageNum3(string source, string target, int[] pageNums) 
        { 
            PdfReader reader = null; 
            Document sourceDocument = null; 
            PdfCopy pdfCopyProvider = null; 
            PdfImportedPage importedPage = null; 
            try 
            { 
                reader = new PdfReader(source); 
                sourceDocument = new Document(reader.GetPageSizeWithRotation(pageNums[0])); 
                pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(target, System.IO.FileMode.Create)); 
                sourceDocument.Open(); 
                foreach (int pageNumber in pageNums) 
                { 
                    importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber); 
                    pdfCopyProvider.AddPage(importedPage); 
                }
                sourceDocument.Close(); 
                reader.Close();
                return true;
            } 
            catch (Exception ex) 
            {
                return false;
            }
        }

        /// <summary>
        /// PDF有多少页拆分成多少个pdf文件
        /// </summary>
        /// <param name="source"></param>
        /// <param name="targetFolder"></param>
        /// <returns>返回值判断是否执行成功</returns>
        public static bool PDFSplit(string source,string targetFolder)
        {
            PdfReader reader = null;
            Document sourceDocument = null;
            PdfCopy pdfCopyProvider = null;
            PdfImportedPage importedPage = null;
            int PageCount = 0;
            try
            {
                reader = new PdfReader(source);
                PageCount = reader.NumberOfPages;

                sourceDocument = new Document(reader.GetPageSizeWithRotation(1));
                for (int i = 1; i <= PageCount; i++)
                {                       
                    pdfCopyProvider = new PdfCopy(sourceDocument, new FileStream(Path.Combine("C://PDF",i+".pdf"), FileMode.Create));
                    sourceDocument.Open();                   
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);   
                }
                sourceDocument.Close();                              
                reader.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            } 
        }

        
    }
}
